// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <memory>
#include <string>

#include <turbo/utility/status.h>
#include <turbo/base/macros.h>
#include <nebula/types/type_fwd.h>

namespace nebula::json {

    /// \brief interface for conversion of Arrays
    ///
    /// Converters are not required to be correct for arbitrary input- only
    /// for unconverted arrays emitted by a corresponding parser.
    class TURBO_EXPORT Converter {
    public:
        virtual ~Converter() = default;

        /// convert an array
        /// on failure, this converter may be promoted to another converter which
        /// *can* convert the given input.
        virtual turbo::Status Convert(const std::shared_ptr<Array> &in,
                               std::shared_ptr<Array> *out) = 0;

        std::shared_ptr<DataType> out_type() const { return out_type_; }

        MemoryPool *pool() { return pool_; }

    protected:
        TURBO_DISALLOW_COPY_AND_ASSIGN(Converter);

        Converter(MemoryPool *pool, const std::shared_ptr<DataType> &out_type)
                : pool_(pool), out_type_(out_type) {}

        MemoryPool *pool_;
        std::shared_ptr<DataType> out_type_;
    };

    /// \brief produce a single converter to the specified out_type
    TURBO_EXPORT turbo::Status make_converter(const std::shared_ptr<DataType> &out_type,
                                      MemoryPool *pool, std::shared_ptr<Converter> *out);

    class TURBO_EXPORT PromotionGraph {
    public:
        virtual ~PromotionGraph() = default;

        /// \brief produce a valid field which will be inferred as null
        virtual std::shared_ptr<Field> Null(const std::string &name) const = 0;

        /// \brief given an unexpected field encountered during parsing, return a type to which
        /// it may be convertible (may return null if none is available)
        virtual std::shared_ptr<DataType> Infer(
                const std::shared_ptr<Field> &unexpected_field) const = 0;

        /// \brief given a type to which conversion failed, return a promoted type to which
        /// conversion may succeed (may return null if none is available)
        virtual std::shared_ptr<DataType> Promote(
                const std::shared_ptr<DataType> &failed,
                const std::shared_ptr<Field> &unexpected_field) const = 0;

    protected:
        TURBO_DISALLOW_COPY_AND_ASSIGN(PromotionGraph);

        PromotionGraph() = default;
    };

    TURBO_EXPORT const PromotionGraph *get_promotion_graph();

}  // namespace nebula::json

